package com.irun.sm.ui.demo.utils;

import java.lang.reflect.Method;

import com.irun.sm.ui.demo.ui.FlowActivity;
import com.irun.sm.ui.demo.vo.AppInfo;

import android.content.ComponentName;
import android.content.Context;

/***
 * @author huangsm
 * @date 2013-1-16
 * @email huangsanm@gmail.com
 * @desc 应用程序启动信息
 */
public class AppUsageState {

	// 淘流量:cn.palminfo.tao.app.IApplication
	private Context mContext;
	public AppUsageState(Context context) {
		mContext = context;
	}

	public int launcherCount(String clsName) {
		int launcherCount = 0;
		ComponentName componentName = new ComponentName(mContext, clsName);
		try {
			// 获得ServiceManager类
			Class<?> ServiceManager = Class.forName("android.os.ServiceManager");
			// 获得ServiceManager的getService方法
			Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
			// 调用getService获取RemoteService
			Object oRemoteService = getService.invoke(null, "usagestats");
			// 获得IUsageStats.Stub类
			Class<?> cStub = Class.forName("com.android.internal.app.IUsageStats$Stub");
			// 获得asInterface方法
			Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
			// 调用asInterface方法获取IUsageStats对象
			Object oIUsageStats = asInterface.invoke(null, oRemoteService);
			// 获得getPkgUsageStats(ComponentName)方法
			Method getPkgUsageStats = oIUsageStats.getClass().getMethod("getPkgUsageStats", android.content.ComponentName.class);
			// 调用getPkgUsageStats 获取PkgUsageStats对象
			Object stats = getPkgUsageStats.invoke(oIUsageStats, componentName);
			// 获得PkgUsageStats类
			Class<?> PkgUsageStats = Class.forName("com.android.internal.os.PkgUsageStats");
			launcherCount = PkgUsageStats.getDeclaredField("launchCount").getInt(stats);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return launcherCount;
	}

	public long usageTime(String clsName) {
		long usageTime = 0l;
		ComponentName componentName = new ComponentName(mContext, clsName);
		try {
			// 获取serviceManager
			Class<?> serviceManager = Class.forName("android.os.ServiceManager");
			// 获取getService
			Method getService = serviceManager.getMethod("getService", java.lang.String.class);
			Object remoteSerice = getService.invoke(null, "usagestats");
			// 获取IUsageStats的Stub方法
			Class<?> stub = Class.forName("com.android.internal.app.IUsageStats$Stub");
			// 获得asInterface方法
			Method asInterface = stub.getMethod("asInterface",android.os.IBinder.class);
			// 调用asInterface方法获取IUsageStats对象
			Object oIUsageStats = asInterface.invoke(null, remoteSerice);
			
			Class<?> cIUsageStatus = Class.forName("com.android.internal.app.IUsageStats");
			// 获得getPkgUsageStats(ComponentName)方法
			Method getPkgUsageStats = cIUsageStatus.getMethod("getPkgUsageStats", android.content.ComponentName.class);
			// 调用getPkgUsageStats 获取PkgUsageStats对象
			Object stats = getPkgUsageStats.invoke(oIUsageStats, componentName);
			// 获得PkgUsageStats类
			Class<?> PkgUsageStats = Class.forName("com.android.internal.os.PkgUsageStats");
			usageTime = PkgUsageStats.getDeclaredField("usageTime").getLong(stats);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return usageTime;
	}

	public void statisticsAllApplication(AppInfo info) {
		try {
			Class<?> cServiceManager = Class.forName("android.os.ServiceManager");
			Method mGetService = cServiceManager.getMethod("getService", java.lang.String.class);
			Object oRemoteService = mGetService.invoke(null, "usagestats");
			Class<?> cStub = Class.forName("com.android.internal.app.IUsageStats$Stub");
			Method mUsageStatsService = cStub.getMethod("asInterface", android.os.IBinder.class);
			Object oIUsageStats = mUsageStatsService.invoke(null, oRemoteService);
			
			Class<?> cIUsageStatus = Class.forName("com.android.internal.app.IUsageStats");
			Method mGetAllPkgUsageStats = cIUsageStatus.getMethod("getAllPkgUsageStats", (Class[]) null);
			Object[] oPkgUsageStatsArray = (Object[]) mGetAllPkgUsageStats.invoke(oIUsageStats, (Object[]) null);
			Class<?> cPkgUsageStats = Class.forName("com.android.internal.os.PkgUsageStats");
			for (Object pkgUsageStats : oPkgUsageStatsArray) {
				String packageName = (String) cPkgUsageStats.getDeclaredField("packageName").get(pkgUsageStats);
				if(packageName.equals(info.getPackageName())){
					int launchCount = cPkgUsageStats.getDeclaredField("launchCount").getInt(pkgUsageStats);
					long usageTime = cPkgUsageStats.getDeclaredField("usageTime").getLong(pkgUsageStats);
					info.setLauncherCount(launchCount);
					info.setUsageTime(usageTime);
					System.out.println(packageName + ":" + launchCount + ":" + usageTime);
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
